package com.ewhale.feitengguest.widget

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.core.content.ContextCompat
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ewhale.feitengguest.R
import com.ewhale.feitengguest.databinding.ItemScreenTipBinding
import com.ewhale.feitengguest.dto.ClassificationDto
import com.simga.library.adapter.BindRecycAdapter
import com.simga.library.utils.LogUtil
import java.util.logging.Logger

/**
 * 作者：SimGa
 * 日期：2019/12/25
 * 时间：11:53
 * 包名：com.ewhale.feitengguest.widget
 */
class ScreenTipDialog(context: Context) {
    private var mContext: Context? = null
    private val popupWindow: MPopuWindow
    private val inflater: LayoutInflater
    private val view: View
    private var meterAdapter: ScreenAdapter? = null

    init {
        mContext = context
        inflater = context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        view = inflater.inflate(R.layout.screenhouse_layout, null)

        popupWindow = MPopuWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        meterAdapter = ScreenAdapter()

        view.findViewById<RecyclerView>(R.id.lv_meter).apply {
            layoutManager = GridLayoutManager(mContext, 4)
            adapter = meterAdapter
        }

        view.findViewById<View>(R.id.popu_ll).setOnClickListener {
            popupWindow.dismiss()
        }
        meterAdapter!!.setOnItemClickListener { _, item, _ ->
            if (listener != null) {
                listener!!.onClick(item)
            }
            popupWindow.dismiss()
        }
    }

    private var listener: OnClickListener? = null

    fun setOnClickListener(listener: OnClickListener) {
        this.listener = listener
    }

    interface OnClickListener {
        fun onClick(item: ClassificationDto)
    }

    fun showAsDropDown(parent: View) {
        popupWindow.showAsDropDown(
                parent)
        val cd = ColorDrawable(0x00000000)
        popupWindow.setBackgroundDrawable(cd)
        popupWindow.isFocusable = true
        popupWindow.isOutsideTouchable = true
        popupWindow.update()
    }

    fun setTipDatas(datas: List<ClassificationDto>) {
        meterAdapter!!.setNewData(datas)
        meterAdapter!!.removeAt(0)
    }

    private var cateId = 0
    fun setCate(cateId: Int) {
        this.cateId = cateId
    }

    inner class ScreenAdapter : BindRecycAdapter<ItemScreenTipBinding, ClassificationDto>() {
        override fun normalViewId(): Int {
            return R.layout.item_screen_tip
        }

        override fun onBindViewHolder(mBinding: ItemScreenTipBinding, itemData: ClassificationDto, holder: RecyclerView.ViewHolder, position: Int) {
            mBinding.itemData = itemData
            mBinding.cateId = cateId
            mBinding.btn.setNormalStroke(1,if(cateId == itemData.id) ContextCompat.getColor(mContext,R.color.main_color) else ContextCompat.getColor(mContext,R.color.text_a8a8a8))
        }
    }
}
